<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-01-12 10:31:53
 * @Last Modified time: 2023-01-12 10:32:53
 */
namespace frank\utils\helper;

/**
 * httpcurl帮助类
 * Class HttpHelper
 * @package frank\utils\helper
 */
class HttpHelper
{
	public static function httpGet(string $url) {  
        //echo "url = ".$url;  
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
        curl_setopt($curl, CURLOPT_URL, $url);  

        $res = curl_exec($curl);  
        curl_close($curl);  
        return $res;  
    }  

    public static function httpPost(string $url,?array $data = null,?array $headers=[])
    {
        $headers = array(
            'Content-Type: application/json',
        );
        $curl = curl_init();
        if( count($headers) >= 1 ){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}